package com.ctshk.rpc.scenic.listenner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.ctshk.rpc.scenic.entity.TicketTypeLuckDraw;
import com.ctshk.rpc.scenic.req.TicketTypeLuckDrawExcel;
import com.ctshk.rpc.scenic.service.ITicketTypeLuckDrawService;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class UploadDataListener extends AnalysisEventListener<TicketTypeLuckDrawExcel> {


    private ITicketTypeLuckDrawService ticketTypeLuckDrawService;

    public UploadDataListener() {

    }
    public UploadDataListener(ITicketTypeLuckDrawService ticketTypeLuckDrawService) {
        this.ticketTypeLuckDrawService = ticketTypeLuckDrawService;
    }


    @Autowired
     TicketTypeLuckDraw typeLuckDraw;

    private List<TicketTypeLuckDrawExcel> ticketList = new ArrayList<>();
    @Override
    public void invoke(TicketTypeLuckDrawExcel ticketTypeLuckDrawExcel, AnalysisContext analysisContext) {

        Integer currentRowNum = analysisContext.getCurrentRowNum();
        if (currentRowNum != 0) {
            ticketList.add(ticketTypeLuckDrawExcel);

        }
        if (ticketList.size() >= 1000) {
            ticketTypeLuckDrawService.saveTicket(ticketList);
            ticketList.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        ticketTypeLuckDrawService.saveTicket(ticketList);
        System.out.println("doAfterAllAnalysed()方法");
    }
}
